import { defineStore } from 'pinia'
import { getUrlQuery } from '../index'

interface USEDICSTORE {
  data: []
  settings: object
}

export const useDictionariesStore = defineStore('dictionariesMap', {
  state(): USEDICSTORE {
    return {
      data: [],
      settings: {}
    }
  },
  actions: {
    setData(data: any, settings?: any) {
      this.data = data
      if (settings) {
        this.settings = JSON.parse(settings)
      }
    },
    getData(key: string, child?: string) {
      if (!key) return []
      let obj: any = this.data.find((e: any) => e.code == key)
      if (child) {
        return obj?.subDic?.find((e: any) => e.code == child) || []
      }
      return obj
    },
    upgrade() {
      try {
        const { memberCode, channelCode } = getUrlQuery()
        let obj = this.getData('other', 'fls-upgrade')?.attribute4
        let flag = false
        if (obj) {
          flag = JSON.parse(obj)?.find((e: any) => {
            if (e.channelCode == channelCode) {
              if (e.open == 1) {
                return !e.memberCode.includes(memberCode)
              }
            }
          })
        }
        return flag
      } catch (e) {
        console.log('upgrade', e)
      }
    },
    payUpgrade() {
      try {
        const { channelCode } = getUrlQuery()
        let obj = this.getData('other', 'fls-pay-upgrade')?.attribute1
        let flag = false
        if (obj) {
          flag = JSON.parse(obj)?.find((e: any) => {
            return e.channelCode.includes(channelCode)
          })
        }
        return !!flag
      } catch (e) {
        return false
        console.log('upgrade', e)
      }
    }
  }
})
